package com.molichuxing.services.business.service.impl;

import com.molichuxing.framework.contants.RedisKeyConst;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.CollectionUtils;
import com.molichuxing.framework.utils.Md5Util;
import com.molichuxing.framework.utils.RedisUtil;
import com.molichuxing.framework.utils.StringUtil;
import com.molichuxing.services.business.service.GroupBizService;
import com.molichuxing.services.infrastructure.dto.response.response.GroupDto;
import com.molichuxing.services.infrastructure.dto.response.response.GroupModifyDto;
import com.molichuxing.services.infrastructure.service.AccountGroupService;
import com.molichuxing.services.infrastructure.service.GroupDesenseService;
import com.molichuxing.services.infrastructure.service.GroupResourceService;
import com.molichuxing.services.infrastructure.service.GroupService;
import com.molichuxing.services.property.GroupSystemTypeEnum;
import com.molichuxing.services.property.StatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色组合服务
 *
 * @author liuruifeng
 * @date 2019年25日
 */
@Service("groupBizService")
public class GroupBizServiceImpl implements GroupBizService {
    private static final Logger logger = LoggerFactory.getLogger(GroupBizServiceImpl.class);

    @Resource
    private GroupService groupService;

    @Resource
    private GroupDesenseService groupDesenseService;

    @Resource
    private GroupResourceService groupResourceService;

    @Resource
    private AccountGroupService accountGroupService;

    @Resource
    private RedisUtil redisUtil;

    // 环境
    @Value("${profiles.active}")
    private String active;

    @Override
    public Paged<GroupDto> getGroupPageList(Map<String, Object> param, int pageNum, int pageSize) {
        int totalCount = groupService.getGroupCountByParam(param);
        if (totalCount > 0) {
            List<GroupDto> groupList = groupService.getGroupPageListByParm(param, pageNum, pageSize);
            return new Paged(groupList, totalCount, pageSize);
        }
        return new Paged(null, totalCount, pageSize);
    }

    /**
     * 创建角色与脱敏字段的关联（先删除后添加）
     *
     * @param groupId  角色id
     * @param desenses 脱敏字段id集合
     * @return
     */
    @Override
    @Transactional
    public int createGroupBindDesense(int groupId, List<Integer> desenses) {
        if (groupId <= 0) {
            throw new IllegalArgumentException("param invalid groupId is" + groupId);
        }

        int row = 0;
        groupDesenseService.deleteByGroupId(groupId);
        if (CollectionUtils.isNotEmpty(desenses)) {
            row = groupDesenseService.createBatchGroupDesense(groupId, desenses);
        }

        return row;
    }

    /**
     * 创建角色与权限的关联（先删除后添加）
     *
     * @param groupId     角色id
     * @param resourceIds 权限id集合
     * @return
     */
    @Override
    @Transactional
    public int createBatchGroupResource(int groupId, List<Integer> resourceIds) {
        if (groupId <= 0) {
            throw new IllegalArgumentException("param invalid groupId is" + groupId);
        }

        int row = 0;
        groupResourceService.deleteByGroupId(groupId);
        if (CollectionUtils.isNotEmpty(resourceIds)) {
            row = groupResourceService.createBatchGroupResource(groupId, resourceIds);
        }

        return row;
    }

    @Override
    @Transactional
    public int modifyGroup(GroupModifyDto dto) {
        if (dto.getId() <= 0) {
            throw new IllegalArgumentException("id is null");
        }

        if (dto.getStatus() == StatusEnum.OFF.getValue()) { //用户被禁用
            Map<String, Object> deleteAccountGroupParam = new HashMap<>();
            deleteAccountGroupParam.put("groupId", dto.getId());
            accountGroupService.deleteByParam(deleteAccountGroupParam); //删除用户与角色的关联关系
        }
        return groupService.modifyGroup(dto);
    }
}
